export const adjustMaps = {
  bfq: 0,
  qfq: 1,
  hfq: 2
}

export const sectorMaps = {
  concept: 'm:90+t:3+f:!50',
  etf: 'b:MK0021,b:MK0022,b:MK0023,b:MK0024',
  index: 'm:1+s:3,m:0+t:5',
  industry: 'm:90+t:2+f:!50',
  stock: 'm:0+t:6,m:0+t:80,m:1+t:2,m:1+t:23,m:0+t:81+s:2048'
}

export const marketMaps = {
  dc: 159, SHFE: 113, CZCE: 115, CFFEX: 220, DCE: 114, INE: 142, GFEX: 225,
  gn: 90, hy: 90, sh: 1, sz: 0, bj: 0
}

export const periodMaps = {
  m1: 1, m5: 5, m15: 15, m30: 30, h: 60,
  d: 101, w: 102, m: 103
}

export function queryParamsEncode(params) {
  let temps = []
  for (let key in params) {
    temps.push(`${key}=${params[key]}`)
  }
  return temps.join('&')
}

export function getTicker(market, symbol, callback) {
  let url = 'https://push2.eastmoney.com/api/qt/stock/get'
  let params = {
    fltt: 2,
    invt: 2,
    fields: 'f43,f44,f45,f46,f47,f48,f49,f50,f51,f52,f57,f58,f59,f60,f71,f84,f85,f92,f107,f108,f116,f117,f119,f127,f152,f161,f162,f163,f164,f165,f167,f168,f169,f170,f172,f173,f174,f175,f176,f198',
    secid: `${marketMaps[market]}.${symbol}`
  }
  fetch(`${url}?${queryParamsEncode(params)}`)
    .then(response => response.json())
    .then(json => {
      callback({
        title: json.data['f58'],
        symbol: json.data['f57'],
        open: json.data['f46'],
        high: json.data['f44'],
        low: json.data['f45'],
        close: json.data['f43'],
        amount: json.data['f48'],
        volume: json.data['f47'],
        turnover_rate: json.data['f168'],
        industry: json.data['f127'],
        pe: json.data['f162'],
        pb: json.data['f167'],
        qr: json.data['f50'],
        pre_close: json.data['f60'],
        total: json.data['f116'],
        float: json.data['f117']
      })
    }).catch(error => {
    throw error
  })
}

export function getSymbols(sector, callback) {
  let url = 'https://push2his.eastmoney.com/api/qt/clist/get'
  let params = {
    pn: 1,
    pz: 10000,
    np: 1,
    fltt: 2,
    invt: 2,
    fs: sectorMaps[sector],
    fields: 'f2,f5,f6,f8,f9,f10,f12,f14,f15,f16,f17,f18,f20,f21,f104,f105,f124,f128,f136,f140'
  }
  fetch(`${url}?${queryParamsEncode(params)}`)
    .then(response => response.json())
    .then(json => {
      callback(json.data.diff.map(item => {
        return {
          title: item['f14'],
          symbol: item['f12'],
          open: item['f17'],
          high: item['f15'],
          low: item['f16'],
          close: item['f2'],
          volume: item['f5'],
          amount: item['f6'],
          turnover_rate: item['f8'],
          pe: item['f9'],
          qr: item['f10'],
          pre_close: item['f18'],
          total: item['f20'],
          float: item['f21'],
          timestamp: item['f124']
        }
      }))
    }).catch(error => {
    throw error
  })
}


export function getStocks(symbol, callback) {
  let url = 'https://push2his.eastmoney.com/api/qt/clist/get'
  let params = {
    pn: 1,
    pz: 2000,
    np: 1,
    fs: `b:${symbol}+f:!50`,
    fields: 'f12'
  }
  fetch(`${url}?${queryParamsEncode(params)}`)
    .then(response => response.json())
    .then(json => {
      callback(json.data.diff.map(item => {
        return item['f12']
      }))
    }).catch(error => {
    throw error
  })
}

export function getKlines({market, symbol, period, adjust = 'qfq'}, callback) {
  let url = 'https://push2his.eastmoney.com/api/qt/stock/kline/get'
  let params = {
    secid: `${marketMaps[market]}.${symbol}`,
    fields1: 'f1,f2,f3,f4,f5,f6',
    fields2: 'f51,f52,f53,f54,f55,f56,f57',
    klt: periodMaps[period],
    fqt: adjustMaps[adjust],
    end: '20500000',
    lmt: 500
  }
  fetch(`${url}?${queryParamsEncode(params)}`)
    .then(response => response.json())
    .then(json => {
      callback(json.data.klines.map(item => {
        let temp = item.split(',')
        return {
          timestamp: new Date(temp[0].replace(new RegExp('-', 'g'), '/')).getTime(),
          open: parseFloat(temp[1]),
          close: parseFloat(temp[2]),
          high: parseFloat(temp[3]),
          low: parseFloat(temp[4]),
          volume: parseFloat(temp[5]),
          amount: parseFloat(temp[6])
        }
      }))
    }).catch(error => {
    throw error
  })
}
